package com.hangim.pce.common.search.service.impl;

import com.hangim.pce.common.search.domain.*;
import com.hangim.pce.common.search.enums.ESActionType;
import com.hangim.pce.common.search.enums.ESEntityType;
import com.hangim.pce.common.search.repository.ESDiseaseRepository;
import com.hangim.pce.common.search.repository.ESDrugRepository;
import com.hangim.pce.common.search.repository.ESElementRepository;
import com.hangim.pce.common.search.repository.ESSymptomRepository;
import com.hangim.pce.common.search.service.ESSyncAmqpService;
import com.hangim.pce.common.search.service.ESSyncService;
import com.hangim.pce.model.BaseDrug;
import com.hangim.pce.model.BaseElement;
import com.hangim.pce.model.SysDisease;
import com.hangim.pce.model.SysSymptom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @see ESSyncService
 */
@Service
public class ESSyncServiceImpl extends BaseElasticSearchServiceImpl implements ESSyncService {
    private final static Logger logger = LoggerFactory.getLogger(ESSyncServiceImpl.class);

    @Autowired
    private ESSyncAmqpService esSyncAmqpService;

    @Autowired
    private ESDiseaseRepository esDiseaseRepository;

    @Autowired
    private ESDrugRepository esDrugRepository;

    @Autowired
    private ESElementRepository esElementRepository;

    @Autowired
    private ESSymptomRepository esSymptomRepository;

    @Override
    public void sendDisease(SysDisease disease, ESActionType actionType) {
        if (disease != null && disease.getId() != null && disease.getId() > 0) {
            ESDisease esDisease = transformDisease(disease);
            ESEntityType entityType = ESEntityType.DISEASE;

            if (ESActionType.SYNC.equals(actionType)) {
                esSyncAmqpService.send(new ESPayLoad(esDisease, actionType, entityType));

            } else if (ESActionType.REMOVE.equals(actionType)) {
                esSyncAmqpService.send(new ESPayLoad(esDisease, actionType, entityType));

            }
        }
    }

    private ESDisease transformDisease(SysDisease disease) {
        ESDisease esDisease = new ESDisease();
        esDisease.setId(disease.getId());
        esDisease.setCode(disease.getCode());
        esDisease.setName(disease.getName());
        esDisease.setSpell(disease.getSpell());
        esDisease.setNameAlias(disease.getNameAlias());
        return esDisease;
    }

    @Override
    public void sendDrug(BaseDrug drug, ESActionType actionType) {
        if (drug != null && drug.getId() != null && drug.getId() > 0) {
            ESDrug esDrug = transformDrug(drug);
            ESEntityType entityType = ESEntityType.DRUG;

            if (ESActionType.SYNC.equals(actionType)) {
                esSyncAmqpService.send(new ESPayLoad(esDrug, actionType, entityType));

            } else if (ESActionType.REMOVE.equals(actionType)) {
                esSyncAmqpService.send(new ESPayLoad(esDrug, actionType, entityType));

            }
        }
    }

    private ESDrug transformDrug(BaseDrug drug) {
        ESDrug esDrug = new ESDrug();
        esDrug.setId(drug.getId());
        esDrug.setName(drug.getName());
        esDrug.setSpell(drug.getSpell());
        esDrug.setNameAlias(drug.getNameAlias());
        return esDrug;
    }

    @Override
    public void sendElement(BaseElement element, ESActionType actionType) {
        if (element != null && element.getId() != null && element.getId() > 0) {
            ESElement esElement = transformElement(element);
            ESEntityType entityType = ESEntityType.ELEMENT;

            if (ESActionType.SYNC.equals(actionType)) {
                esSyncAmqpService.send(new ESPayLoad(esElement, actionType, entityType));

            } else if (ESActionType.REMOVE.equals(actionType)) {
                esSyncAmqpService.send(new ESPayLoad(esElement, actionType, entityType));

            }
        }
    }

    private ESElement transformElement(BaseElement element) {
        ESElement esElement = new ESElement();
        esElement.setId(element.getId());
        esElement.setName(element.getName());
        esElement.setSpell(element.getSpell());
        esElement.setNameAlias(element.getNameAlias());
        return esElement;
    }

    @Override
    public void sendSymptom(SysSymptom symptom, ESActionType actionType) {
        if (symptom != null && symptom.getId() != null && symptom.getId() > 0) {
            ESSymptom esSymptom = transformSymptom(symptom);
            ESEntityType entityType = ESEntityType.ELEMENT;

            if (ESActionType.SYNC.equals(actionType)) {
                esSyncAmqpService.send(new ESPayLoad(esSymptom, actionType, entityType));

            } else if (ESActionType.REMOVE.equals(actionType)) {
                esSyncAmqpService.send(new ESPayLoad(esSymptom, actionType, entityType));

            }
        }
    }

    private ESSymptom transformSymptom(SysSymptom symptom) {
        ESSymptom esSymptom = new ESSymptom();
        esSymptom.setId(symptom.getId());
        esSymptom.setCode(symptom.getCode());
        esSymptom.setName(symptom.getName());
        esSymptom.setSpell(symptom.getSpell());
        esSymptom.setNameAlias(symptom.getNameAlias());
        return esSymptom;
    }

    @Override
    public void syncDisease(ESDisease esDisease) {
        try {
            esDiseaseRepository.save(esDisease);
        } catch (Exception e) {
            logger.error("fail to save ESDisease to elasticsearch.", e);
        }
    }

    @Override
    public void deleteDisease(Long Id) {
        try {
            esDiseaseRepository.delete(Id);
        } catch (Exception e) {
            logger.error("fail to delete ESDisease from elasticsearch.", e);
        }
    }

    @Override
    public void syncDrug(ESDrug esDrug) {
        try {
            esDrugRepository.save(esDrug);
        } catch (Exception e) {
            logger.error("fail to save ESDrug to elasticsearch.", e);
        }
    }

    @Override
    public void deleteDrug(Long Id) {
        try {
            esDrugRepository.delete(Id);
        } catch (Exception e) {
            logger.error("fail to delete ESDrug from elasticsearch.", e);
        }
    }

    @Override
    public void syncSymptom(ESSymptom esSymptom) {
        try {
            esSymptomRepository.save(esSymptom);
        } catch (Exception e) {
            logger.error("fail to save ESSymptom to elasticsearch.", e);
        }
    }

    @Override
    public void deleteSymptom(Long Id) {
        try {
            esSymptomRepository.delete(Id);
        } catch (Exception e) {
            logger.error("fail to delete ESSymptom from elasticsearch.", e);
        }
    }

    @Override
    public void syncElement(ESElement esElement) {
        try {
            esElementRepository.save(esElement);
        } catch (Exception e) {
            logger.error("fail to save ESElement to elasticsearch.", e);
        }
    }

    @Override
    public void deleteElement(Long Id) {
        try {
            esElementRepository.delete(Id);
        } catch (Exception e) {
            logger.error("fail to delete ESElement from elasticsearch.", e);
        }
    }

}
